package com.itheima.mynetty.codec;

import com.itheima.netty.pojo.UserInfo;
import com.itheima.netty.util.ProtostuffUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.util.List;

public class ProtoStuffEncoder extends MessageToMessageEncoder<UserInfo> {
    @Override
    protected void encode(ChannelHandlerContext ctx, UserInfo msg, List<Object> out) throws Exception {
        byte[] bytes = ProtostuffUtil.serialize(msg);
        ByteBuf buf = ctx.alloc().buffer();
        buf.writeBytes(bytes);
        out.add(buf);
    }
}
